<%@page import="com.shsh.safemanage.model.ProblemHSECategory"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="com.shsh.safemanage.model.JobPost"%>
<%@page import="com.shsh.safemanage.dao.TitlePostManageGroupDao"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.shsh.safemanage.dao.DepartmentDao"%>
<%@page import="com.shsh.safemanage.dao.PostCheckDao"%>
<%@page import="com.shsh.safemanage.model.Department1"%>
<%@page import="com.shsh.safemanage.model.PostCheck"%>
<%@page import="com.shsh.safemanage.model.PostcheckPattern"%>
<%@page import="com.shsh.safemanage.model.PostcheckContent"%>
<%@page import="com.shsh.safemanage.model.PostcheckStandard"%>
<%@page import="com.shsh.safemanage.model.ProblemTransfer"%>
<%@page import="java.util.List"%>
<%@page import="com.shsh.safemanage.util.Util"%>
<%@page import="com.shsh.safemanage.model.Problem"%>
<%@page import="com.shsh.safemanage.dao.DaoCreator"%>
<%@page import="com.shsh.safemanage.dao.ProblemDao"%>
<%@page import="com.shsh.safemanage.util.Constants"%>

<%
session.removeAttribute(Constants.SESSION_MANAGE_GROUP_USER);
session.removeAttribute(Constants.SESSION_PROBLEM_DEPARTMENT1);

String problemIDStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
Long problemID = 0L;
try {
	problemID = new Long(problemIDStr);
} catch (Exception e) {
}

String transferIDStr = request.getParameter(Constants.REQUEST_PARAMETER_TRANSFER_ID);
Long transferID = 0L;
try {
	transferID = new Long(transferIDStr);
} catch (Exception e) {
}

String fromPage = request.getParameter(Constants.REQUEST_PARAMETER_FROM_PAGE);

DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
ProblemDao problemDao = DaoCreator.createProblemDao();
PostCheckDao postcheckDao = DaoCreator.createPostCheckDao();
TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
WriteProblemAction writeProblemAction = ActionCreator.createWriteProblemAction();
UserAction userAction = ActionCreator.createUserAction();

Problem problem = problemDao.getProblemByID(problemID);

PostCheck postcheck = null;
PostcheckPattern postcheckPattern = null; 
PostcheckContent postcheckContent = null; 
PostcheckStandard postcheckStandard = null; 

if(problem.getProblemType() == Constants.PROBLEM_TYPE_POSTCHECK) {
	postcheck = postcheckDao.getPostCheck(problem.getPostcheckID());
	postcheckPattern = postcheckDao.getPostcheckPatternByID(problem.getPostcheckPatternID());
	postcheckContent =  postcheckDao.getPostcheckContentByID(problem.getPostcheckContentID());
	postcheckStandard = postcheckDao.getPostcheckStandardByID(problem.getPostcheckStandardID());
}

User user = (User) session.getAttribute(Constants.SESSION_USER);
JobPost post1 = titlePostManageGroupDao.getJobPostByID(user.getJobPost1ID());
JobPost post2 = titlePostManageGroupDao.getJobPostByID(user.getJobPost2ID());
JobPost post3 = titlePostManageGroupDao.getJobPostByID(user.getJobPost3ID());
Role theRole = userAction.getUserRole(request);

int role = writeProblemAction.calRole(post1, post2, post3, theRole);
%>
<div class="hider" id="hider">
	<span>&nbsp;</span>
</div>
<div class="position">
	<img src="images/position2.jpg" style="padding: 7px 9px; float: left;" />当前位置：上海高桥分公司问题管理平台 &gt;&gt; 处理问题
</div>

<div class="tt">问题浏览及反馈卡</div>
<div class="content">
	<%@include file="problem_detail_section.jsp"%>
	<table align="center" border="1" width="99%" cellpadding="0"
		cellspacing="0">
		<%if(problem.getProblemType() == Constants.PROBLEM_TYPE_POSTCHECK) {%>
		<tr id="postcheckTR">
			<td class="td1">
			<span style="margin-left: 30px" class="span1">岗检：</span><%=postcheck.getCheckName() %>
			<br/>
			<span style="margin-left: 30px" class="span1">检查方式：</span><%=postcheckPattern.getPattern() %>
			<br/>
			<span style="margin-left: 30px" class="span1">工作内容：</span><%=postcheckContent.getContent() %>
			<br/>
			<span style="margin-left: 30px" class="span1">工作标准与要求：</span><%=postcheckStandard.getStandard() %>
			<br/>
			<span style="margin-left: 30px" class="span1">扣分值：</span>
			<%
				if(theRole.getRoleLevel() < Constants.ROLE_LEVEL_KEJI) {
			%>
			<%=problem.getGrade() == null ? "&nbsp;":Util.formatGrade(problem.getGrade()) %>
			<%} else {%>
			<input id="grade" type="text" value="<%=problem.getGrade() == null ? "":Util.formatGrade(problem.getGrade()) %>"/>
			<%} %>
			<br/>
			<span style="margin-left: 30px" class="span1">要求整改完成日期：</span><%=Util.formatDate(problem.getCompleteDate()) %>
			</td>
		</tr>
		<%} %>
		<tr>
			<td class="td1">
				<span style="margin-left: 30px; font-weight: bold">反馈信息：</span>
				<br />
				<div id="feedbackProcessMethodDiv">
				<%@include file="feedback_process_method.jsp" %>
				</div>
				<%
				Department2 userDepart2 = departmentDao.getDepartment2ByID(user.getDepartment2ID());
				if (fromPage != null && (fromPage.equals(Constants.FROM_PAGE_YIBAN_PAGE) || fromPage.equals(Constants.FROM_PAGE_POSTCHECK_BROWSE))) {
					if ((role & Constants.ROLE_TYPE_YIXIANYUANGONG) == 0 || 
							(userDepart2.isEquip() && userDepart2.getEquipID().equals(problem.getEquipmentID()))) {
				%>
				<textarea style="width: 600px; height: 100px; margin-left: 60px" id="feedback"></textarea>
				<%
					}
				} else {
				%>
				<textarea style="width: 600px; height: 100px; margin-left: 60px" id="feedback"></textarea>
				<%
				}
				%>
			</td>
		</tr>
		<tr id="informedPersonsTR">
			<td class="td1">
				<%
				if ((role & Constants.ROLE_TYPE_ZUZHANG) > 0) {
				%>
				<div id="informPersonDiv">
				<%@include file="problem_feedback_informed_person.jsp" %>
				</div>
				<%
				}
				%>
				<%
				if ((role & (Constants.ROLE_TYPE_ZHUANGZHIZHANG | Constants.ROLE_TYPE_ZUZHANG | Constants.ROLE_TYPE_SHENGCHANFUBUZHANG
						| Constants.ROLE_TYPE_SHEBEIFUBUZHANG | Constants.ROLE_TYPE_BUZHANG)) > 0) {
				%>
				<div id="chooseManageGroupUserDiv">
				<%@include file="managegroup_select/manage_check_samepage_section.jsp"%>
				</div>
				<%
				}
				%>
				<%
				if ((role & (Constants.ROLE_TYPE_KEYUAN | Constants.ROLE_TYPE_KEZHANG | Constants.ROLE_TYPE_FUKEZHANG 
						| Constants.ROLE_TYPE_FUCHUZHANG | Constants.ROLE_TYPE_CHUZHANG)) > 0) {
				%>
				<div id="chooseChushiTransferDiv">
				<%@include file="chushi_informed_person.jsp"%>
				</div>
				<%
				}
				%>
			</td>
		</tr>
		<tr>
			<td class="td1">
				<span style="margin-left: 30px" class="span1">填报人及时间：</span>
				<span><%=user.getDepartment1Name()%>-<%=user.getDepartment2Name()%>-<%=user.getDepartment3Name()%></span>
				<span><%=user.getName()%></span>
				<span><%=Util.formatDateTime(Calendar.getInstance())%></span>
			</td>
		</tr>
		<tr>
			<td class="td1" align="center">
				<input type="button" value="提 交" onclick="feedback()" />
				<input type="button" value="取 消" style="margin-left: 20px" onclick="cancelFeedback()" />
			</td>
		</tr>
	</table>
</div>

<script type="text/javascript">
function feedback() {
	var feedback = getElementValueByID('feedback');
	if (feedback == '') {
		$.dialog.alert('请输入反馈意见');
		return;
	}
	
	var processMethod = 0;
	var processRadio0 = document.getElementById('processRadio0');
	var processRadio1 = document.getElementById('processRadio1');
	var processRadio2 = document.getElementById('processRadio2');
	var processRadio3 = document.getElementById('processRadio3');
	var processRadio4 = document.getElementById('processRadio4');
	var processRadio5 = document.getElementById('processRadio5');
	var processRadio6 = document.getElementById('processRadio6');
	if (processRadio1 != null && processRadio1.checked) {
		processMethod = 1;
	} else if (processRadio2 != null && processRadio2.checked) {
		processMethod = 2;
	} else if (processRadio3 != null && processRadio3.checked) {
		processMethod = 3;
	} else if (processRadio4 != null && processRadio4.checked) {
		processMethod = 4;
	} else if (processRadio5 != null && processRadio5.checked) {
		processMethod = 5;
	} else if (processRadio6 != null && processRadio6.checked) {
		processMethod = 6;
	} else if (processRadio0 != null && processRadio0.checked) {
		processMethod = 0;
	}
	
	var userIDs = '';
	var userIDElements = document.getElementsByName('informedUserID');
	for (var i = 0; i != userIDElements.length; i++) {
		var userIDEle = userIDElements[i];
		if (userIDEle.checked) {
			if (userIDs == '') {
				userIDs = userIDs + userIDEle.value;
			} else {
				userIDs = userIDs + ',' + userIDEle.value;
			}
		}
	}
	
	var manageGroupUserCheckEles = document.getElementsByName('manageGroupUserCheck');
	for (var i = 0; i != manageGroupUserCheckEles.length; i++) {
		if (manageGroupUserCheckEles[i].checked) {
			if (userIDs == '') {
				userIDs = userIDs + manageGroupUserCheckEles[i].value;
			} else {
				userIDs = userIDs + ',' + manageGroupUserCheckEles[i].value;
			}
		}
	}

	var categoryPerPosID = '';
	var categoryPerEquipID = '';
	var categoryWorkActID = '';
	var categoryToolEquipID = '';
	var categoryWorkEnvID = '';
	<%
	if (problem.getProblemType() == null || problem.getProblemType().equals(0)) {
	%>
	var categoryPerPosCheckEles = document.getElementsByName('categoryPerPos');
	for (var i = 0; i != categoryPerPosCheckEles.length; i++) {
		if (categoryPerPosCheckEles[i].checked) {
			if (categoryPerPosID == '') {
				categoryPerPosID = categoryPerPosCheckEles[i].value;
			} else {
				$.dialog.alert('请仅选择一个人员位置');
				return;
			}
		}
	}
	
	var categoryPerEquipCheckEles = document.getElementsByName('categoryPerEquip');
	for (var i = 0; i != categoryPerEquipCheckEles.length; i++) {
		if (categoryPerEquipCheckEles[i].checked) {
			if (categoryPerEquipID == '') {
				categoryPerEquipID = categoryPerEquipCheckEles[i].value;
			} else {
				$.dialog.alert('请仅选择一个装备配合或使用不当');
				return;
			}
		}
	}
	
	var categoryWorkActCheckEles = document.getElementsByName('categoryWorkAct');
	for (var i = 0; i != categoryWorkActCheckEles.length; i++) {
		if (categoryWorkActCheckEles[i].checked) {
			if (categoryWorkActID == '') {
				categoryWorkActID = categoryWorkActCheckEles[i].value;
			} else {
				$.dialog.alert('请仅选择一个作业行为');
				return;
			}
		}
	}
	
	var categoryToolEquipCheckEles = document.getElementsByName('categoryToolEquip');
	for (var i = 0; i != categoryToolEquipCheckEles.length; i++) {
		if (categoryToolEquipCheckEles[i].checked) {
			if (categoryToolEquipID == '') {
				categoryToolEquipID = categoryToolEquipCheckEles[i].value;
			} else {
				$.dialog.alert('请仅选择一个工具及设备');
				return;
			}
		}
	}
	
	var categoryWorkEnvCheckEles = document.getElementsByName('categoryWorkEnv');
	for (var i = 0; i != categoryWorkEnvCheckEles.length; i++) {
		if (categoryWorkEnvCheckEles[i].checked) {
			if (categoryWorkEnvID == '') {
				categoryWorkEnvID = categoryWorkEnvCheckEles[i].value;
			} else {
				$.dialog.alert('请仅选择一个作业场所与环境');
				return;
			}
		}
	}
	
	if (categoryPerPosID == '' && categoryPerEquipID == '' && categoryWorkActID == '' 
		&& categoryToolEquipID == '' && categoryWorkEnvID == '') {
		$.dialog.alert('请选择问题分类');
		return;
	}
	<%
	}
	%>

	
	<%
	if(problem.getProblemType() == Constants.PROBLEM_TYPE_POSTCHECK && theRole.getRoleLevel() >= Constants.ROLE_LEVEL_KEJI) {
	%>
	var grade = null; 
	grade = getElementValueByID('grade');
		
	if(grade == null ||  grade == '' || grade == 'undefined') {
		$.dialog.alert('请在扣分值输入一个数字');
		return;
	}
		
	var strExp = <%=Constants.REGX_GRADE%>;
	if (!strExp.test(grade)){
		$.dialog.alert('扣分值必须为非负数');
		return;
	}
	
	$.dialog.confirm('您确定反馈此问题？', function() {
		openWaitingDialog('waitingDialog');
		$.ajax({
			type:'post',
			url:'feedback_problem_process.jsp',
			data:{
				processMethod: processMethod,
				id: <%=problemID%>,
				feedback: getElementValueByID('feedback'),
				transferID: <%=transferID%>,
				userID: userIDs,
				grade: grade
			},
			success:function(data,state,xmlhttp){
				var obj = null;
				try {
					obj = eval('(' + data + ')');
				} catch (e) {
				}
				if (typeof(obj.isSuccess) != 'undefined') {
					closeWaitingDialog('waitingDialog');
					if (obj.isSuccess == 'true') {
						$.dialog('问题反馈成功！', function() {
							if ('<%=fromPage%>' == 'yiban') {
								toPage = '<%=request.getContextPath()%>/problem_browse.jsp';
							} else if ('<%=fromPage%>' == 'postcheckBrowse') {
								toPage = '<%=request.getContextPath()%>/postcheck_browse.jsp';
								loadPageSection(toPage, 'mainContentDiv', {fromPage: 'feedback', postcheckID: <%=problem.getPostcheckID()%>});
								return;
							}  else if ('<%=fromPage%>' == 'postcheckReport') {
								toPage = '<%=request.getContextPath()%>/postcheck/report/postcheck_user_daily_report.jsp';
								loadPageSection(
										toPage, 
										'mainContentDiv', 
										{
											fromPage: '<%=request.getParameter(Constants.REQUEST_PARAMETER_ENTER_PAGE)%>', 
											postcheckID: <%=problem.getPostcheckID()%>,
											depart1ID: <%=request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID)%>,
											userID: <%=request.getParameter(Constants.REQUEST_PARAMETER_USER_ID)%>,
											year: <%=request.getParameter(Constants.REQUEST_PARAMETER_YEAR)%>, 
											month: <%=request.getParameter(Constants.REQUEST_PARAMETER_MONTH)%>,
											day: <%=request.getParameter(Constants.REQUEST_PARAMETER_DAY)%>,
											dayStart: <%=request.getParameter(Constants.REQUEST_PARAMETER_DAY_START)%>,
											dayEnd: <%=request.getParameter(Constants.REQUEST_PARAMETER_DAY_END)%>						
										}
								);
								return;								
							} else {
								toPage = '<%=request.getContextPath()%>/problems_tobe_processed.jsp';
							}
							loadPageSection(toPage, 'mainContentDiv', {fromPage: 'feedback'}); 
						});
					} else {
						$.dialog.alert(obj.error);
					}
				}
			}
		});
	});	
	<%
	} else {
	%>
	$.dialog.confirm('您确定反馈此问题？', function() {
		openWaitingDialog('waitingDialog');
		$.ajax({
			type:'post',
			url:'feedback_problem_process.jsp',
			data:{
				processMethod: processMethod,
				id: <%=problemID%>,
				feedback: getElementValueByID('feedback'),
				transferID: <%=transferID%>,
				userID: userIDs,
				categoryPerPosID: categoryPerPosID,
				categoryPerEquipID: categoryPerEquipID,
				categoryWorkActID: categoryWorkActID,
				categoryToolEquipID: categoryToolEquipID,
				categoryWorkEnvID: categoryWorkEnvID,
				categoryPerPosOther: getElementValueByID('categoryPerPosOther'),
				categoryPerEquipOther: getElementValueByID('categoryPerEquipOther'),
				categoryWorkActOther: getElementValueByID('categoryWorkActOther'),
				categoryToolEquipOther: getElementValueByID('categoryToolEquipOther'),
				categoryWorkEnvOther: getElementValueByID('categoryWorkEnvOther')
			},
			success:function(data,state,xmlhttp){
				var obj = null;
				try {
					obj = eval('(' + data + ')');
				} catch (e) {
				}
				if (typeof(obj.isSuccess) != 'undefined') {
					closeWaitingDialog('waitingDialog');
					if (obj.isSuccess == 'true') {
						$.dialog('问题反馈成功！', function() {
							if ('<%=fromPage%>' == 'yiban') {
								toPage = '<%=request.getContextPath()%>/problem_browse.jsp';
							} else if ('<%=fromPage%>' == 'postcheckBrowse') {
								toPage = '<%=request.getContextPath()%>/postcheck_browse.jsp';
								loadPageSection(toPage, 'mainContentDiv', {fromPage: 'feedback', postcheckID: <%=problem.getPostcheckID()%>});
								return;								
							}  else if ('<%=fromPage%>' == 'postcheckReport') {
								toPage = '<%=request.getContextPath()%>/postcheck/report/postcheck_user_daily_report.jsp';
								loadPageSection(
										toPage, 
										'mainContentDiv', 
										{
											fromPage: '<%=request.getParameter(Constants.REQUEST_PARAMETER_ENTER_PAGE)%>', 
											postcheckID: <%=problem.getPostcheckID()%>,
											depart1ID: <%=request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID)%>,
											userID: <%=request.getParameter(Constants.REQUEST_PARAMETER_USER_ID)%>,
											year: <%=request.getParameter(Constants.REQUEST_PARAMETER_YEAR)%>, 
											month: <%=request.getParameter(Constants.REQUEST_PARAMETER_MONTH)%>,
											day: <%=request.getParameter(Constants.REQUEST_PARAMETER_DAY)%>,
											dayStart: <%=request.getParameter(Constants.REQUEST_PARAMETER_DAY_START)%>,
											dayEnd: <%=request.getParameter(Constants.REQUEST_PARAMETER_DAY_END)%>						
										}
								);
								return;		
							} else {
								toPage = '<%=request.getContextPath()%>/problems_tobe_processed.jsp';
							}
							loadPageSection(toPage, 'mainContentDiv', {fromPage: 'feedback'}); 
						});
					} else {
						$.dialog.alert(obj.error);
					}
				}
			}
		});
	});
	<%
	}
	%>
}

function cancelFeedback() {
	$.dialog.confirm('您确定取消此次反馈吗？', function() {
		var toPage = '';
		if ('<%=fromPage%>' == 'yiban') {
			toPage = '<%=request.getContextPath()%>/problem_browse.jsp';
		} else if ('<%=fromPage%>' == 'postcheckBrowse') {
			toPage = '<%=request.getContextPath()%>/postcheck_browse.jsp';
			loadPageSection(toPage, 'mainContentDiv', {fromPage: 'feedback', postcheckID: <%=problem.getPostcheckID()%>}, true);
			return;
		}  else if ('<%=fromPage%>' == 'postcheckReport') {
			toPage = '<%=request.getContextPath()%>/postcheck/report/postcheck_user_daily_report.jsp';
			loadPageSection(
					toPage, 
					'mainContentDiv', 
					{
						fromPage: '<%=request.getParameter(Constants.REQUEST_PARAMETER_ENTER_PAGE)%>', 
						postcheckID: <%=problem.getPostcheckID()%>,
						depart1ID: <%=request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID)%>,
						userID: <%=request.getParameter(Constants.REQUEST_PARAMETER_USER_ID)%>,
						year: <%=request.getParameter(Constants.REQUEST_PARAMETER_YEAR)%>, 
						month: <%=request.getParameter(Constants.REQUEST_PARAMETER_MONTH)%>,
						day: <%=request.getParameter(Constants.REQUEST_PARAMETER_DAY)%>,
						dayStart: <%=request.getParameter(Constants.REQUEST_PARAMETER_DAY_START)%>,
						dayEnd: <%=request.getParameter(Constants.REQUEST_PARAMETER_DAY_END)%>						
					},
					true
			);
			return;
		} else if ('<%=fromPage%>' == 'supervise') {
			toPage = '<%=request.getContextPath()%>/problems_tobe_processed_supervise.jsp';
		} else {		
			toPage = '<%=request.getContextPath()%>/problems_tobe_processed.jsp';
		}
		loadPageSection(toPage, 'mainContentDiv', {fromPage: 'feedback'}, true);
	}, function(){});
}

function chooseProcessMethod(method) {
	if (method == 1) {
		document.getElementById('informedPersonsTR').style.display = 'none';
		document.getElementById('feedback').readonly = true;
		setElementValueByID('feedback', '<%=user.getDepartment1Name()%>，<%=user.getDepartment3Name()%><%=user.getName()%>确认该问题已整改');
	} else if (method == 2) {
		document.getElementById('informedPersonsTR').style.display = '';
		document.getElementById('feedback').readonly = false;
		setElementValueByID('feedback', '');
	} else if (method == 4 || method == 3 || method == 5 || method == 6 || method == 0) {
		document.getElementById('informedPersonsTR').style.display = 'none';
		setElementValueByID('feedback', '');
	}
}
</script>
